#define _CRT_SECURE_NO_WARNINGS
#include"sqeue.h"


void sqeueinit(sqeue* sq)
{
	assert(sq);
	sq->sqhead = sq->sqtail = NULL;
}

void sqeuepush(sqeue* sq, sqdatatype x)
{
	assert(sq);
	sqeuenode* newnode = (sqeuenode*)malloc(sizeof(sqeuenode));
	if (newnode == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (sq->sqhead == NULL)
	{
		sq->sqhead = sq->sqtail = newnode;
	}
	else
	{
		sq->sqtail->next = newnode;
		sq->sqtail = sq->sqtail->next;

		 
	}

}
bool sqeueEmpty(sqeue* sq)
{
	assert(sq);
	return sq->sqhead == NULL;
}

void sqeuepop(sqeue* sq)
{
	assert(!sqeueEmpty(sq));
	if (sq->sqhead == sq->sqtail)
	{
		free(sq->sqhead);
		sq->sqhead = sq->sqtail = NULL;
	}
	else
	{
		sqeuenode* next = sq->sqhead->next;
		free(sq->sqhead);
		sq->sqhead = next;
	}
}

sqdatatype sqeuetail(sqeue* sq)
{
	assert(!sqeueEmpty(sq));
	return sq->sqtail->data;
}

sqdatatype sqeuehead(sqeue* sq)
{
	assert(!sqeueEmpty(sq));
	return sq->sqhead->data;
}

void sqeuedestroy(sqeue* sq)
{
	sqeuenode* pcur = sq->sqhead;
	while (pcur != NULL)
	{
		sqeuenode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	sq->sqhead = sq->sqtail = NULL;
}